//
//  SCTabBarItem.swift
//  weibo
//
//  Created by 阿城 on 15/12/12.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCTabBarItem: UITabBarItem {
    
    override var badgeValue: String?  {
        didSet{
            
            let vc = valueForKey("_target") as! UITabBarController
            
            let view = getSubview("_UIBadgeBackground", fromView: vc.tabBar)
            
            view?.setValue(UIImage(named: "main_badge"), forKey: "_image")
            
        }
    }
    
    private func getSubview(name: String, fromView: UIView)->UIView? {
        
        if let cls = NSClassFromString(name){
            
            if fromView.subviews.count == 0{
                return nil
            }
            
            for subview in fromView.subviews{
                if subview.isKindOfClass(cls){
                    return subview
                }else{
                    let view = getSubview(name, fromView: subview)
                    if view != nil{
                        return view
                    }
                }
            }
        }
        return nil
    }
    
}